// 将权限信息映射成菜单数据
export function authorityMapToMenu (data) {
  if (data instanceof Array && data.length > 0) {
    const menuTree = (node, fullPath = []) => {
      // if (Object.hasOwnProperty.call(node, 'menuType') && node.menuType === 0) {
      //   if (node.path && typeof node.path === 'string') {
      //     fullPath.push(node.path)
      //   }
      //   if (fullPath.length) {
      //     node.fullPath = fullPath.join('/')
      //   }
      // }
      // if (Object.hasOwnProperty.call(node, 'children') && node.children instanceof Array) {
      //   node.children = node.children
      //     .map(d => menuTree(d, fullPath))
      //     .filter(d => d.path)
      //     .sort((a, b) => a.sort > b.sort ? 1 : -1)
      // }

      if (Object.hasOwnProperty.call(node, 'menuType')) {
        if (node.path && typeof node.path === 'string') {
          node.fullPath = node.path
        }
      }

      if (node instanceof Array) {
        node = node
          .map(d => menuTree(d, fullPath))
          .filter(d => d.path)
          .sort((a, b) => a.sort > b.sort ? 1 : -1)
      } else {
        if (Object.hasOwnProperty.call(node, 'children') && node.children instanceof Array) {
          node.children = node.children
            .map(d => menuTree(d, fullPath))
            .filter(d => d.path)
            .sort((a, b) => a.sort > b.sort ? 1 : -1)
        }
      }

      return node
    }
    // const menus = menuTree(data[0])
    // return menus.children instanceof Array ? menus.children : []
    const menus = menuTree(data)
    return menus instanceof Array ? menus : []
  }
  return undefined
}

// 从权限信息中抽离按钮权限
export function authorityMapToPermissions (data) {
  const permissions = []
  if (data instanceof Array && data.length > 0) {
    const menuTree = (node, fullPath = []) => {
      if (Object.hasOwnProperty.call(node, 'menuType')) {
        // if (node.menuType === 0) {
        //   if (node.path && typeof node.path === 'string') {
        //     fullPath.push(node.path)
        //   }
        //   if (fullPath.length) {
        //     node.fullPath = fullPath.join('/')
        //     permissions.push(`/${node.fullPath}`)
        //   }
        // }
        if (node.path && typeof node.path === 'string') {
          node.fullPath = node.path
          permissions.push(`/${node.fullPath}`)
        }
      }
      if (node instanceof Array) {
        node = node
          .map(d => menuTree(d, [...fullPath]))
          .filter(d => d.path)
      } else {
        if (Object.hasOwnProperty.call(node, 'children') && node.children instanceof Array) {
          node.children = node.children
            .map(d => menuTree(d, [...fullPath]))
            .filter(d => d.path)
        }
      }

      return node
    }
    // menuTree(data[0])
    menuTree(data)
    return permissions
  }
  return undefined
}
